package com.airtight.game;

import com.airtight.objects.Background;
import com.airtight.objects.Ball;
import com.airtight.objects.ClickableObject;
import com.airtight.objects.Column;
import com.airtight.objects.Shard;

import java.util.Random;

import static android.opengl.Matrix.translateM;

public class Objects {
    public float ratio;

    public Background background;
    public Ball ball;
    public Shard shard;

    public Column[] left_columns;
    public Column[] right_columns;
    public Column[] bottom_columns;
    public Column[] top_columns;


    public ClickableObject[] clickObjects = new ClickableObject[4];

    Objects(float ratio){
        this.ratio = ratio;
        initMenuObjects(ratio);
        initWorldObjects(ratio);
    }

    private void initMenuObjects(float ratio){
        spawnBackground(ratio);
        spawnPlates(ratio);
    }

    private void initWorldObjects(float ratio){
        //float altratio = 1/ratio;
        spawnColumns(ratio);
        spawnBall(ratio);
        spawnShard(ratio);

    }

    private void spawnBackground(float ratio){
        background = new Background(0,0,1,1);
    }

    private void spawnPlates(float ratio){
        float altratio = 1/ratio;
        if(ratio >= 1){
            for(int i = 0; i<4;i++){
                clickObjects[i] = new ClickableObject(0, 0.2f - 0.25f*i,0.5f,0.2f/ratio);
            }
        }
        else{
            for(int i = 0; i<4;i++){
                clickObjects[i] = new ClickableObject(0, 0.2f - 0.25f*i,0.5f,0.2f/altratio);
            }
        }
    }

    private void spawnBall(float ratio){
        float hz = top_columns.length;
        ball = new Ball(0,0, 1/hz, 1/hz/ratio);
    }

    public void spawnShard(float ratio){
        float vl = left_columns.length + 2;
        float hz = top_columns.length;
        Random random = new Random();
        shard = new Shard(0,0, 1/hz, 1/hz/ratio);

        float x = random.nextFloat()*2/3f;
        float y = random.nextFloat()*-13/16f;

        if(ball.getX() <= 0){
            translateM(shard.shardMatrix, 0, x, 0, 0);
        }
        else{
            translateM(shard.shardMatrix, 0, -x, 0, 0);
        }
        translateM(shard.shardMatrix, 0, 0, y, 0);
    }

    public void columnDimensions(){
        int vertical = 14;
        int horizontal = 9;

        if(ratio == 16/9f){
            vertical = 14;
            horizontal = 9;
        }
        else if(ratio == 5/3f){
            vertical = 8;
            horizontal = 6;
        }

        else if(ratio == 8/5f){
            vertical = 14;
            horizontal = 10;
        }

        else if(ratio == 3/2f){
            vertical = 10;
            horizontal = 8;
        }

        else if(ratio == 4/3f){
            vertical = 10;
            horizontal = 9;
        }

        left_columns = new Column[vertical];
        right_columns = new Column[vertical];
        bottom_columns = new Column[horizontal];
        top_columns = new Column[horizontal];
    }

    private void spawnColumns(float ratio){
        Random random = new Random();
        columnDimensions();
        float hz = left_columns.length + 2;
        float vl = top_columns.length;
        for(int l = 0; l < left_columns.length; l++) {
            left_columns[l] = new Column(0, 0, 1/vl, 1/vl/ratio);
            left_columns[l].setX(-random.nextFloat()*2/vl - 1f);
            left_columns[l].setY(-1 + 3/ hz + l*(2/ hz));
        }

        for(int r = 0; r < right_columns.length; r++) {
            right_columns[r] = new Column(0, 0, 1/vl, 1/vl/ratio);
            right_columns[r].setX(random.nextFloat()*2/vl + 1f);
            right_columns[r].setY(-1 + 3/ hz + r*(2/ hz));
        }

        for(int t = 0; t < top_columns.length; t++) {
            top_columns[t] = new Column(0, 0, 1/vl, 1/vl/ratio);
            top_columns[t].setX(-1 + 1/vl + t*(2/vl));
            top_columns[t].setY(1 - 1/ hz);
        }

        for(int b = 0; b < bottom_columns.length; b++) {
            bottom_columns[b] = new Column(0, 0, 1/vl, 1/vl/ratio);
            bottom_columns[b].setX(-1 + 1/vl + b*(2/vl));
            bottom_columns[b].setY(-1 + 1/ hz);
        }

    }

}
